﻿using QuickChat_Service.Repositories.Database;
using QuickChat_Service.Repositories.Repositories.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QuickChat_Service.Repositories.Repositories.Implementations
{
    public class EFChatRoomRepository : EFRepository<ChatRoom>, IChatRoomRepository
    {
        public EFChatRoomRepository(QuickChatApplicationEntities context) : base(context) { }

        #region Interface Implementation

        public override ChatRoom GetById(int chatRoomId)
        {
            try
            {
                return DbSet.Include("Talkers").SingleOrDefault(room => room.RoomId == chatRoomId);
            }
            catch (InvalidOperationException ex)
            {
                throw new NotImplementedException("Its not possible to get more than two rooms with the same id.", ex);
            }
        }

        public void CreateNewChatRoom(int roomOwnersId)
        {
            ChatRoom newRoom = new ChatRoom()
            {
                CreatedTime = DateTime.UtcNow,
                CreatorsId = roomOwnersId,
                Talkers = new List<Talker>()
            };

            base.Add(newRoom);
        }

        public override void Update(ChatRoom entity, int targetId)
        {
            throw new Exception("Cannot edit a rooms owner once it has been created");
        }

        #endregion
    }
}